# 2008-11-27
# author:jiangchanghui
# libvirt network
class NetworkXml:
	"""class to represent an network in libvirt"""
	def __init__ ( self, name, uuid=""):
		self.name = name
		self.uuid = uuid
		self.forward = {}
		self.bridge = {"name":"virbr0"}
		self.ip = {}
		self.dhcp = {}
                self.model = {}
                self.bootScripts=[];
                self.shutdownScripts=[];
       
        def setShutdownScripts(self,scripts):
                self.shutdownScripts = scripts;
        
        def shutdownScriptsToXml(self):
                xml = "<shutdownScript>\n";
                for script in self.shutdownScripts:
                    xml+="\t"+script+"\n";
                xml +="</shutdownScript>"
                return xml;

        def setBootScripts(self,bootScripts):
                self.bootScripts = bootScripts;
        def bootScriptsToXml(self):
                xml = "<bootScript>\n";
                for script in self.bootScripts:
                    xml+="\t"+script+"\n";
                xml +="</bootScript>"
                return xml;

	def setBridge( self, name,stp="off",forwardDelay="0"):
		self.bridge['name'] = name
		self.bridge['stp'] = stp
		self.bridge["forwardDelay"] = forwardDelay

	def setForward( self, mode="",dev=""):
		self.forward['mode'] = mode
		self.forward['dev'] = dev
	
	def setIP (self, address, mask):
		self.ip['address'] = address
		self.ip['mask'] = mask

	def setDhcp(self, start, end):
		self.dhcp['start'] = start
		self.dhcp['end'] = end

        def setModel(self,type):
                self.model['type']=type

	def bridgeToXml(self):
		return  '<bridge name="'+self.bridge["name"]+'" stp="'+self.bridge['stp']+'" forwardDelay="'+self.bridge['forwardDelay']+'"/>'

	def forwardToXml(self):
		xml = ""
		keys = self.forward.keys()
		if(len(keys)==0):
			return ""
		xml = "<forward "
		if(self.forward['mode']!=""):
			xml += ' mode="'+self.forward['mode']+'" '

		if(self.forward['dev']!=""):
		        xml += ' dev="'+self.forward['dev']+'" '

		xml += " />"

		return xml
	def dhcpToXml(self):
		xml = ""
		keys = self.dhcp.keys();
		if(len(keys)==0):
			return ""

		try:
			xml = '''<dhcp>\n\t\t<range start="'''+self.dhcp['start']+'" end="'+self.dhcp['end']+'''" />\n\t</dhcp>'''
			return xml
		except KeyError:
			return ""
	def ipToXml(self):
		try:
			xml = ""
			xml = '<ip address="'+self.ip['address']+'" netmask="'+self.ip['mask']+'">'
			xml += "\n\t"+self.dhcpToXml();
			xml += "\n\t</ip>"
			return xml
		except KeyError:
			return ""
        def modelToXml(self):
                try:
                       xml = ""
                       xml = "<model type='"+self.model['type']+"' />"
                       return xml
                except KeyError:
                       return ""

	def toXml(self):
		try:
			xml = ""
			xml = """<network>\n\t<name>"""+self.name+"</name>"
			if(self.uuid!=""):
				xml += "\n\t<uuid>"+self.uuid+"</uuid>"
			xml += "\n\t"+self.bridgeToXml();
			xml += "\n\t"+self.forwardToXml();
			xml += "\n\t"+self.ipToXml();
                        xml += "\n\t"+self.modelToXml();
                        xml += "\n"+self.bootScriptsToXml();
                        xml += "\n"+self.shutdownScriptsToXml();  
                        xml += "\n</network>"
			return xml
		except KeyError:
			return ""

